unit JATUIF_Driver_Mouse;

{$mode objfpc}{$H+}

interface

uses
    Classes, SysUtils, Mouse;

Type
    TMouseDriver_Callback_Click = procedure(X,Y:Integer;Left:Boolean) of Object;
    TMouseDriver_Callback_Move = procedure(X,Y:Integer) of Object;

    { TMouseDriver }

    TMouseDriver = class(TObject)
      Public
        Constructor Create(Callback_Click:TMouseDriver_Callback_Click; Callback_Move:TMouseDriver_Callback_Move);
        Procedure Poll;
      Private
        DriverCallback_Click:TMouseDriver_Callback_Click;
        DriverCallback_Move:TMouseDriver_Callback_Move;
        MouseEvent:TMouseEvent;
        LastPosX:Integer;
        LastPosY:Integer;
    end;

implementation

{ TMouseDriver }

constructor TMouseDriver.Create(Callback_Click:TMouseDriver_Callback_Click; Callback_Move:TMouseDriver_Callback_Move);
begin
     LastPosX:=0;
     LastPosY:=0;
     DriverCallback_Click:= Callback_Click;
     DriverCallback_Move:= Callback_Move;
     InitMouse;
end;

procedure TMouseDriver.Poll;
Var
   MsX,MsY:Integer;

begin
     If PollMouseEvent(MouseEvent) then begin
        GetMouseEvent(MouseEvent);
        MsX:= MouseEvent.x+1;
        MsY:= MouseEvent.y+1;
        If MouseEvent.Action = MouseActionMove then begin
           If (MsX <> LastPosX) or (MsY <> LastPosY) then begin
              LastPosX:= MsX;
              LastPosY:= MsY;
              DriverCallback_Move(MsX,MsY);
              Exit;
           end else begin
               Exit;
           end;
        end;
        If MouseEvent.Action <> MouseActionMove then begin
           If MouseEvent.buttons = MouseLeftButton then begin
              DriverCallback_Click(MsX,MsY,true);
              Exit;
           end else If MouseEvent.Buttons = MouseRightButton then begin
               DriverCallback_Click(MsX,MsY,false);
               Exit;
           end;
        end;
     end;
end;

end.

